package org.nighthawkhosting.SC2Mapster;

import java.util.ArrayList;

import org.apache.http.cookie.Cookie;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ThreadView extends ListActivity {

	private ArrayList<ForumThread> threadList;
	private ForumThreadAdapter adapter;
	private String url = "";
	private String title = "";
	private ArrayList<Cookie> cookieData = new ArrayList<Cookie>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState == null) {
			Bundle extras = getIntent().getExtras();
			url = extras.getString("_url");
			title = extras.getString("_title");
			
			Bundle cookieBundle = extras.getBundle("_cookies");
			for(int i = 0; i < cookieBundle.size(); i++) {
				cookieData.add(CookieParser.parse(cookieBundle.getStringArrayList(String.valueOf(i))));
			}
		} else {
			url = (String) savedInstanceState.getSerializable("_url");
			title = (String) savedInstanceState.getSerializable("_title");
			
			Bundle cookieBundle = savedInstanceState.getBundle("_cookies");
			for(int i = 0; i < cookieBundle.size(); i++) {
				cookieData.add(CookieParser.parse(cookieBundle.getStringArrayList(String.valueOf(i))));
			}
		}
		setContentView(R.layout.threadlist);
		setTitle(this.title);
		fillData();
	}

	private void fillData() {
		ForumThreadParser parser = new ForumThreadParser();
		parser.setURL(this.url);
		parser.setCookieData(this.cookieData);
		threadList = parser.parse();
		this.adapter = new ForumThreadAdapter(this, R.layout.threadrow, threadList);
		setListAdapter(this.adapter);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable("_url", this.url);
		outState.putSerializable("_title", this.title);
		
		Bundle cookieBundle = new Bundle();
		for (int i2 = 0; i2 < cookieData.size(); i2++) {			
			cookieBundle.putStringArrayList(String.valueOf(i2), CookieParser.parse(cookieData.get(i2)));
		}
		outState.putBundle("_cookies", cookieBundle);
	}


	@Override
	protected void onResume() {
		super.onResume();
		//fillData();
	}

	public class ForumThreadAdapter extends ArrayAdapter<ForumThread> {

		private ArrayList<ForumThread> forumThreads;

		public ForumThreadAdapter(Context context, int textViewResourceId, ArrayList<ForumThread> items) {
			super(context, textViewResourceId, items);
			this.forumThreads = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ForumThread data = forumThreads.get(position);
			View v = convertView;

			if (data == null) return v;

			LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.threadrow, null);
			
			ImageView image = (ImageView) v.findViewById(R.id.icon);
			TextView title = (TextView) v.findViewById(R.id.threadtitle);
			TextView poster = (TextView) v.findViewById(R.id.threadposter);
			
			if(data.getForumThreadStatus().equals("Deleted")) {
				image.setImageResource(R.drawable.deleted);
				title.setText(data.getForumThreadTitle());
				poster.setText(data.getForumThreadPoster().trim());
				return v;
			} else if(data.getForumThreadStatus().equals("New posts"))
				image.setImageResource(R.drawable.unread);
			else {
				image.setImageResource(R.drawable.read);
			}
			
			title.setText(data.getForumThreadTitle());
			poster.setText("Posted by: " + data.getForumThreadPoster());

			return v;
		}
	}
}
